#include "adts.h"
#include "stdio.h"
#include "stdlib.h"
#include "aac.h"

/*
profile 1 AAC-MAIN 2 AAC-LC
samplereate :
0: 96000 Hz
1: 88200 Hz
2: 64000 Hz
3: 48000 Hz
4: 44100 Hz
5: 32000 Hz
6: 24000 Hz
7: 22050 Hz
8: 16000 Hz
9: 12000 Hz
10: 11025 Hz
11: 8000 Hz
12: 7350 Hz
channel:
0: Defined in AOT Specifc Config
1: 1 channel: front-center
2: 2 channels: front-left, front-right
*/
int parse_asc(char *AudioSpecificConfig, int size,  uint8_t *profile, uint8_t *samplerate, uint8_t *channel ) {
    if (size < 2) {
        return -1;
    }
    uint8_t profile_ObjectType =  AudioSpecificConfig[0];
    uint8_t samplingFrequencyIndex = AudioSpecificConfig[1];
    
    uint8_t aac_channels = (samplingFrequencyIndex >> 3) & 0x0f;
    samplingFrequencyIndex = ((profile_ObjectType << 1) & 0x0e) | ((samplingFrequencyIndex >> 7) & 0x01);
    profile_ObjectType = (profile_ObjectType >> 3) & 0x1f;
    
    // set the aac sample rate.
    uint8_t aac_sample_rate = samplingFrequencyIndex;
    *samplerate = samplingFrequencyIndex;
    *channel = aac_channels;
    *profile = profile_ObjectType ; 
    return 0;
}
